package com.woniu.book.config;

import com.ruoyi.common.constant.UserConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

import static com.ruoyi.common.constant.UserConstants.DELAY_ORDER_QUEUE;

@Configuration
public class MqConfig {


    //普通队列(绑定死信交换机)
    @Bean
    public Queue normalQueue(){
        HashMap<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("x-message-ttl",172800000); //48小时
        paramsMap.put("x-dead-letter-exchange",UserConstants.DLX_ORDER_EXCHANGE);//死信交换机名称
        paramsMap.put("x-dead-letter-routing-key",UserConstants.DLX_ORDER_ROUTING_KEY);//死信routing key

        return new Queue(DELAY_ORDER_QUEUE,true,false,false,paramsMap);
    }

    //死信队列
    @Bean
    public Queue dedQueue(){
        return new Queue(UserConstants.DLX_ORDER_QUEUE,true,false,false);
    }

    //普通交换机
    @Bean
    public DirectExchange normalDirectExchange(){
        return new DirectExchange(UserConstants.DELAY_ORDER_EXCHANGE,true,false);
    }

    //死信交换机
    @Bean
    public DirectExchange dedDirectExchange(){
        return new DirectExchange(UserConstants.DLX_ORDER_EXCHANGE,true,false);
    }

    //绑定普通队列
    @Bean
    public Binding normalBinding(@Qualifier("normalDirectExchange") DirectExchange directExchange,@Qualifier("normalQueue") Queue queue){
             return BindingBuilder.bind(queue).to(directExchange).with(UserConstants.DELAY_ORDER_ROUTING_KEY);
    }

    //绑定死信队列
    @Bean
    public Binding dedBinding(@Qualifier("dedDirectExchange") DirectExchange directExchange,@Qualifier("dedQueue") Queue queue){
        return BindingBuilder.bind(queue).to(directExchange).with(UserConstants.DLX_ORDER_ROUTING_KEY);
    }
}
